package com.fanh.controller;

import com.alibaba.fastjson.JSONObject;
import com.fanh.common.api.CommonResult;
import com.fanh.common.util.SmsCodeUtil;
import com.fanh.dto.UserInfoDetails;
import com.fanh.model.SysUser;
import com.fanh.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "UserInfoController", description = "用户管理")
@RequestMapping("/a/app")
public class UserInfoController {


    @Autowired
    private UserService userService;

    @ApiOperation("手机号注册")
    @RequestMapping(value = "/sys/user/phoneRegister", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult phoneRegister(@ApiParam(value = "短信发送后返回的sessionID",required = true)@RequestParam("sessionId") String sessionId,
                                      @ApiParam(value = "手机号码",required = true)@RequestParam("phone") String phone,
                                      @ApiParam(value = "短信验证码",required = true) @RequestParam("verificationCode") String verificationCode,
                                      @ApiParam(value = "用户姓名，不能用于登录",required = true)@RequestParam("name") String name,
                                      @ApiParam(value = "用户密码",required = true)@RequestParam("password") String password) {
        if (StringUtils.isAnyBlank(phone,verificationCode)){
            return CommonResult.failed("验证码或手机号不能为空");
        }
      JSONObject jsonObject = SmsCodeUtil.verifySms(sessionId, verificationCode);

        String code = jsonObject.getString("code");
        if (!"200".equals(code)) {
            return CommonResult.failed("验证码错误");
        }
       SysUser user = userService.phoneRegist(phone,name,password);

        return CommonResult.success("注册成功");
     }
    @ApiOperation("检查手机号是否注册")
    @RequestMapping(value = "/checkPhone", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult checkPhone(@ApiParam(value = "手机号码",required = true)@RequestParam(value ="phone") String phone) {
        if (StringUtils.isAnyBlank(phone)){
            return CommonResult.failed("手机号不能为空！");
        }
        SysUser user =new SysUser();
        user.setPhone(phone);
      List<SysUser> users= userService.getUser(user);
        if(users!=null && users.size()>0){
            return CommonResult.success(1,"该手机号已注册");
        }
        return CommonResult.success(0,"该手机号未注册");
    }

    @ApiOperation("用户名密码登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@ApiParam(value = "登录名或手机号码",required = true)@RequestParam(value = "username") String username,
                              @ApiParam(value = "用户密码",required = true)@RequestParam(value = "password") String password,
                              @ApiParam(value = "验证码 没有用到")@RequestParam(value = "validateCode" ,required = false) String validateCode) {
        Map<String, Object> resultMap = new HashMap<>();
        String token = userService.login(username, password);
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        resultMap.put("token", token);
        resultMap.put("bx_video", false);
        return CommonResult.success(resultMap,"登录成功");
    }

    @ApiOperation("修改用户信息")
    @RequestMapping(value = "/sys/user/modifyUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult modifyUserInfo(SysUser user, @RequestParam(required = false) MultipartFile file) {

        return CommonResult.success(1);
    }
    @ApiOperation("忘记密码")
    @RequestMapping(value = "/sys/user/modifyPassword", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult modifyPassword(@ApiParam(value = "手机号码",required = true)@RequestParam("phone") String phone,
                                       @ApiParam(value = "验证码发送返回的SSESSION",required = true)@RequestParam("sessionId") String sessionId,
                                       @ApiParam(value = "验证码",required = true) @RequestParam("verificationCode") String verificationCode,
                                       @ApiParam(value = "新密码",required = true) @RequestParam("newPassword") String newPassword) {
        if (StringUtils.isBlank(verificationCode)|| StringUtils.isBlank(sessionId)) {
            return CommonResult.failed("验证码不能为空");
        }

       int count = userService.modifyPassword(phone,sessionId,verificationCode,newPassword);
        if(count >0){
            return CommonResult.success("修改密码成功");
        }else{
            return CommonResult.failed("修改密码失败");
        }


    }
    @ApiOperation("发送验证码")
    @RequestMapping(value = "/sys/user/sendVerificationCode", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult sendVerificationCode(@ApiParam(value = "手机号码",required = true)@RequestParam("phone") String phone) {

        if (StringUtils.isBlank(phone)) {
            return CommonResult.failed("号码不能为空");
        }
        JSONObject jsonObject = SmsCodeUtil.sendSms(phone);
        return CommonResult.success(jsonObject);
    }



    @ApiOperation("微信登录/注册")
    @RequestMapping(value = "/wxLogin", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult wxLogin(@ApiParam(required = true)String accessToken,
                                @ApiParam(required = true)String openId,
                                @ApiParam(required = false)String unionId,
                                @ApiParam(value = "1--学生 2--教师", required = false)@RequestParam(name = "type", defaultValue = "1", required = false) Integer type) {
        Map<String, Object> map   = userService.wxlogin(accessToken,openId);
        return CommonResult.success(map);
    }


    @ApiOperation("绑定微信")
    @RequestMapping(value = "/wxBind", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult wxBind(@ApiParam(required = true)String accessToken, @ApiParam(required = true)String openId) {
        Map<String, String> tokenMap = new HashMap<>();
        int  count = userService.bingWx(accessToken,openId);
        if (count>0) {
            return CommonResult.success(count, "绑定成功！");
        }else{
            return CommonResult.failed("绑定失败，请重试!");
        }

    }


}

